/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

import com.aphrodite.ui.graphics.GraphicUtil;

//completed
public final class ScrollBar extends Widget {// implements Scrollable {

	private int availableHeight;
	private int availableWidth;
	private int barBGClr = 0xD4D0C8;
	private int barBorderClr = 0x606060;
	private int barFGColor = 0x989591;
	private int barLength = 50;
	private int barThickness = 7;
	private short layout = LAYOUT_HORIZONTAL;

	private int percent;
	private int scrollBarX;

	private int scrollBarY;
	private int scrollBoxHeight;
	private int scrollBoxWidth;

	public ScrollBar() {
		super(null, 0, 0);
		// setDims();
		setLayout(layout);

		setPercent(0);
	}

	public void cleanUp() {
		scrollBoxHeight = 0;
		scrollBoxWidth = 0;
		availableHeight = 0;
		availableWidth = 0;
		scrollBarX = 0;
		scrollBarY = 0;
	}

	public int getPercent() {

		return percent;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		// -------- draw the scroll bar -------
		g.setColor(barBGClr);
		GraphicUtil.fillRect(g, 200, 0, 0, width, height);
		g.setColor(barFGColor);
		g.fillRect(scrollBarX, scrollBarY, scrollBoxWidth, scrollBoxHeight);

		g.setColor(0xffffff);
		// right side line
		g.drawLine(scrollBarX, scrollBarY, scrollBarX, scrollBarY + scrollBoxHeight);
		// top line
		g.drawLine(scrollBarX, scrollBarY, scrollBarX + scrollBoxWidth, scrollBarY);
		g.setColor(0x000000);
		if (layout == LAYOUT_HORIZONTAL) {
			// bottom line
			g.drawLine(scrollBarX, scrollBarY + scrollBoxHeight, scrollBarX + scrollBoxWidth - 1, scrollBarY
					+ scrollBoxHeight - 1);
			// right line
			g.drawLine(scrollBarX + scrollBoxWidth, scrollBarY, scrollBarX + scrollBoxWidth, scrollBarY
					+ scrollBoxHeight - 1);
		} else {
			g.drawLine(scrollBarX + width - 1, scrollBarY, scrollBarX + width - 1, scrollBarY + scrollBoxHeight - 1);
			g.drawLine(scrollBarX, scrollBarY + scrollBoxHeight - 1, scrollBarX + width - 1, scrollBarY
					+ scrollBoxHeight - 1);
		}
		release(g, width, height, false);

	}

	public void setBarDimensions(int thickness, int length) {
		this.barThickness = thickness;
		this.barLength = length;
		// setDims();
		setLayout(layout);
		setPercent(percent);
		reDraw();
	}

	public void setLayout(short layout) {
		this.layout = layout;
		updateModel();
		scrollBoxHeight = (layout == LAYOUT_VERTICAL) ? height * 10 / 100 : width - 1;
		scrollBoxWidth = (layout == LAYOUT_HORIZONTAL) ? width * 10 / 100 : width - 1;
		availableHeight = height - scrollBoxHeight;
		availableWidth = width - scrollBoxWidth;
	}

	public void setPercent(int percent) {
		if (percent <= 100) {
			if (layout == LAYOUT_VERTICAL) {
				scrollBarY = (availableHeight * percent) / 100;
				scrollBarX = 0;
			} else {
				scrollBarY = 0;
				scrollBarX = (availableWidth * percent) / 100;
			}
		}
	}

	protected void updateModel() {
		if (layout == LAYOUT_HORIZONTAL) {
			this.width = barLength;
			this.height = barThickness;
		} else {
			this.width = barThickness;
			this.height = barLength;
		}
	}

}
